﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class ProblemHolder : MonoBehaviour {
    public string personName = "Put a crazy name";
    public Problem problem;

    private GameObject canvas;
    private GameObject pname;
    private GameObject bmenu;

    void Start()
    {
        canvas = GameObject.Find("Canvas");
        
        foreach (Transform t in canvas.transform)
        {
            if (t.name == "PersonName")
            {
                pname = t.gameObject;
            }
            if (t.name == "BottonMenu")
            {
                bmenu = t.gameObject;
            }
        }

        pname.SetActive(false);
        bmenu.SetActive(false);
    }

    public void SetDraw()
    {
        pname.SetActive(true);
        pname.GetComponentInChildren<Text>().text = personName;
        bmenu.SetActive(true);
        bmenu.GetComponentInChildren<Text>().text = problem.question;
        Button[] buts = bmenu.GetComponentsInChildren<Button>();
        for (int i = 0; i < buts.Length; i++)
        {
            buts[i].GetComponentInChildren<Text>().text = problem.solutions[i].solution;
            buts[i].onClick.RemoveAllListeners();

            // gambiarra do demonho, precisa por o número fixo pro código não usar o valor de i que estiver no final e cagar tudo
            if (i == 0)
                buts[i].onClick.AddListener(() => { ButtonClicked(0); });
            if (i == 1)
                buts[i].onClick.AddListener(() => { ButtonClicked(1); });
            if (i == 2)
                buts[i].onClick.AddListener(() => { ButtonClicked(2); });
        }


    }

    void ButtonClicked(int index)
    {
        if (problem.solutions[index].effects[0].effect[0] + Status.GetIndice(Status.Indices.Money) >= 0)
        {
            float cost = problem.solutions[index].effects[0].effect[0];
            problem.solutions[index].effects[0].effect[0] = 0;

            Status.AddIndice(Status.Indices.Money, cost);

            problem.selectedSolution = index;
            problem.ApplySelectedSolution();
            GetComponent<PersonComportament>().StartGetOut();
            GameObject.Find("Manager").GetComponent<ProblemManager>().ShowHideCorrupt(false);
            Close();
        }
    }
    public void Close()
    {
        pname.SetActive(false);
        bmenu.SetActive(false);
    }
}
